package xhwl.insurance.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.ArrayList;

import xhwl.android.common.util.StringUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.R;
import xhwl.insurance.customView.PlayerWebView;
import xhwl.insurance.dialog.VideoShareDialog;
import xhwl.insurance.http.bean.resp.VideoListResp;
import xhwl.insurance.util.XHLog;

/**
 * Created by fuyi on 2017/9/26.
 */

public class FindAdapter extends RecyclerView.Adapter<FindAdapter.MyViewHolder> {
    private Context context;
    public static final int TYPE_HEADR = 1;  //说明是带有header的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的
    private View mHeadrView;
    private FindAdapter.OnItemClickLitener mOnItemClickLitener;
    private ArrayList<VideoListResp> mlistVideo;
    private VideoShareDialog shareDialog;

    public FindAdapter(Context context, ArrayList<VideoListResp> mlistVideo) {
        this.context = context;
        this.mlistVideo = mlistVideo;
    }

    @Override
    public FindAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (mHeadrView != null && viewType == TYPE_HEADR) {
            return new MyViewHolder(mHeadrView);
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_find, null);
        return new MyViewHolder(layout);
    }

    @Override
    public void onViewRecycled(MyViewHolder holder) {
        super.onViewRecycled(holder);

    }

    @Override
    public void onBindViewHolder(final FindAdapter.MyViewHolder holder, final int position) {
        XHLog.e("onBindViewHolder position="+position);
        XHLog.e("onBindViewHolder first visible position=");
        if (getItemViewType(position) == TYPE_NORMAL) {

            if (holder instanceof MyViewHolder) {
                holder.mFindTitle.setText(mlistVideo.get(position - 1).getVIDEO_NAME());
                holder.mTime.setText(mlistVideo.get(position - 1).getCREATE_DATE());
//                int pos = holder.getLayoutPosition();
                final int pos=position-1;
                final VideoListResp video=mlistVideo.get(pos);
                holder.videoPlayer.setCoverImageUrl(video.getVIDEO_PIC());
                holder.videoPlayer.setVideoUrl(video.getVIDEO_URL());
                if (StringUtil.isEmpty(video.getTHUMBS_UP())) {
                    holder.mGoodNum.setText("0");
                }else {
                    holder.mGoodNum.setText(video.getTHUMBS_UP());
                }

                if(video.isPlaying){
                    holder.videoPlayer.play();
                }
                else
                {
                    holder.videoPlayer.pause();
                }
                holder.videoPlayer.setCallBack(new PlayerWebView.CallBack() {
                    @Override
                    public void onPlayBtnClick() {
                        for(int i=0;i<mlistVideo.size();i++){
                            if(pos==i){
                                mlistVideo.get(i).isPlaying=true;
                            }
                            else {
                                mlistVideo.get(i).isPlaying=false;
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
                if (mOnItemClickLitener != null) {
                    holder.llItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            mOnItemClickLitener.onItemClick(position-1);
                        }
                    });
                }

                intGoods(holder);//点击点赞
                initmVideoShare(holder);//视频分享

                return;
            } else {
                return;
            }
        } else if (getItemViewType(position) == TYPE_HEADR) {
            return;
        }
    }

    private void initmVideoShare(MyViewHolder holder) {
        holder.mVideoShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shareDialog = new VideoShareDialog(context);
                shareDialog.setCancelable(false);
                shareDialog.setCanceledOnTouchOutside(false);
                View dialog = View.inflate(context, R.layout.dialog_videoshare, null);
                RelativeLayout mWxCircle = (RelativeLayout) dialog.findViewById(R.id.rl_wxcircle);
                RelativeLayout mWechat = (RelativeLayout) dialog.findViewById(R.id.rl_wechat);
                TextView mCancel = (TextView) dialog.findViewById(R.id.btn_videocancel);
                shareDialog.setContentView(dialog);
                shareDialog.show();

                mWxCircle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new ShareAction((Activity)context)
                                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .setCallback(shareListener)
//                                .withText("hello")
//                                .withMedia(image)
                                .share();
                    }
                });

                mWechat.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new ShareAction((Activity) context)
                                .setPlatform(SHARE_MEDIA.WEIXIN)
                                .setCallback(shareListener)
//                                .withText("hello")
//                                .withMedia(image)
                                .share();
                    }
                });

                mCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        shareDialog.dismiss();
                    }
                });
            }
        });
    }

    private void intGoods(final MyViewHolder holder) {
        holder.mGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if (mHeadrView != null) {
            return mlistVideo.size() + 1;
        } else {
            return mlistVideo.size();
        }
    }


    public View getHeadView() {
        return mHeadrView;
    }

    public void setHeadView(View headView) {
        mHeadrView = headView;
        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADR;
        }
        return TYPE_NORMAL;
    }

    public void pause() {
        for(VideoListResp resp:mlistVideo){
            resp.isPlaying=false;
        }
        notifyDataSetChanged();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {


        PlayerWebView videoPlayer;
        LinearLayout llItem;
        ImageView mGoods;
        ImageView mVideoShare;
        TextView mGoodNum;
        TextView mFindTitle;
        TextView mTime;

        public MyViewHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeadrView) {
                return;
            }
            llItem=(LinearLayout) itemView.findViewById(R.id.llItem);
            mGoods = (ImageView) itemView.findViewById(R.id.iv_find_good);
            mVideoShare = (ImageView) itemView.findViewById(R.id.iv_videoshare);
            mGoodNum = (TextView) itemView.findViewById(R.id.tv_find_goodnum);
            mFindTitle = (TextView) itemView.findViewById(R.id.tv_find_title);
            videoPlayer = (PlayerWebView) itemView.findViewById(R.id.videoWebView);
            mTime = (TextView) itemView.findViewById(R.id.tv_find_time);

        }

    }

    public interface OnItemClickLitener {
        void onItemClick(int position);
    }

    public void setOnItemClickLitener(FindAdapter.OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    private UMShareListener shareListener = new UMShareListener() {

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showToast(context,"分享成功！");
            if(shareDialog!=null&&shareDialog.isShowing()){
                shareDialog.dismiss();
            }
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.showToast(context,"分享失败！");
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.showToast(context,"取消分享");

        }
    };

}
